package cheesymock;

import java.lang.reflect.*;
import java.util.*;
import static cheesymock.MethodUtil.*;

/**
 * Verifies that all methods on the delegate object has been
 * called. Does not verify property access, though.
 *
 * @author fredrik
 */
public class MandatoryInvocation extends Validator {
	private Set<Method> invoked = new HashSet<Method>();
	
	@Override
	public void invocation(Method method, Object[] arguments) {
		if (delegate != null) {
			try {
				Method m = resolveMethod(method, method.getParameterTypes(), this.delegate);
				invoked.add(m);
			}
			catch (NoSuchMethodException e) {
			}
			catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	}

	@Override
	public void check() {
		if (delegate != null) {
			for(Method m: delegate.getClass().getDeclaredMethods()) {
				if (!invoked.contains(m)) {
					throw new IllegalStateException("Method " + m.getName() + 
							" is declared in the delegate object, but did not get called " + 
							" during execution.");
				}
			}
		}
	}
}